<?php

namespace App\Controller\Exam;

use App\Dao\Exam\ExamDao;
use App\Middleware\AuthTokenMiddleware;
use App\Service\Exam\ExamService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Middleware;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;

/**
 * @ApiController(prefix="exam")
 * @Middleware(AuthTokenMiddleware::class)
 */
class ExamController extends BaseController
{
    /**
     * @Inject()
     * @var ExamService
     */
    protected $examService;

    /**
     * @PostApi(path="lists")
     */
    public function lists()
    {
        $data = $this->examService->lists();
        _SUCCESS($data);
    }

    /**
     * @PostApi(path="detail")
     * @FormData(key="exam_id",rule="required")
     */
    public function detail()
    {
        $data = $this->getValidatorData();
        $data = $this->examService->detail($data->get('exam_id'));
        _SUCCESS($data);
    }

    /**
     * @PostApi(path="today")
     */
    public function today()
    {
        $user_id = $this->getUid();
        $res = $this->examService->today($user_id);
        _SUCCESS($res);
    }


    /**
     * @PostApi(path="question")
     * @FormData(key="exam_id",rule="required")
     */
    public function question()
    {
        $exam_id = $this->getValidatorData('exam_id');
        $data = $this->examService->question(1);
        _SUCCESS($data);
    }

    /**
     * @PostApi(path="start")
     * @FormData(key="exam_test_id",rule="required")
     * @FormData(key="exam_id",rule="required")
     */
    public function start()
    {
        $data = $this->getValidatorData();
        $this->examService->start($data->get('exam_test_id'), $data->get('exam_id'));
        _SUCCESS();
    }

    /**
     * @PostApi(path="submit")
     * @FormData(key="exam_id",rule="required")
     * @FormData(key="result",rule="required")
     */
    public function submit()
    {
        $data = $this->getValidatorData();
        $data->offsetSet('user_id', $this->getUid());
        $this->examService->result($data);
        _SUCCESS();
    }

    /**
     * @PostApi(path="report")
     * @FormData(key="exam_test_id",rule="required")
     */
    public function report()
    {
        $exam_test_id = $this->getValidatorData('exam_test_id');
        $res = $this->examService->report($exam_test_id);
        _SUCCESS($res);
    }

    /**
     * @PostApi(path="join")
     * @FormData(key="tel",rule="required")
     * @FormData(key="exam_id",rule="required")
     * @FormData(key="realname",rule="required")
     * @FormData(key="gender",rule="required")
     */
    public function join()
    {
        $data = $this->getValidatorData();
        $this->examService->join($data);
        _SUCCESS();
    }

}